package com.qdlc.p2p.dal.dao.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.qdlc.p2p.common.util.DateUtil;
import com.qdlc.p2p.common.util.StringUtil;
import com.qdlc.p2p.dal.common.dao.jpa.BaseDaoImpl;
import com.qdlc.p2p.dal.common.model.jpa.OrderFilter.OrderType;
import com.qdlc.p2p.dal.common.model.jpa.PageDataList;
import com.qdlc.p2p.dal.common.model.jpa.QueryParam;
import com.qdlc.p2p.dal.common.model.jpa.SearchFilter;
import com.qdlc.p2p.dal.common.model.jpa.SearchFilter.Operators;
import com.qdlc.p2p.dal.dao.TppMerchantLogDao;
import com.qdlc.p2p.dal.dto.TppMerchantLog;
import com.qdlc.p2p.dal.model.TppMerchantLogModel;

@Repository("tppMerchantLogDao")
public class TppMerchantLogDaoImpl extends BaseDaoImpl<TppMerchantLog> implements TppMerchantLogDao {

	@Override
	public PageDataList<TppMerchantLogModel> list(TppMerchantLogModel model) {
		QueryParam param = QueryParam.getInstance();
		param.addPage(model.getPage(), model.getRows());
		if(!StringUtil.isBlank(model.getSearchName())){
			SearchFilter orFilter1 = new SearchFilter("operator.userName", Operators.LIKE, model.getSearchName());
    		param.addOrFilter(orFilter1, orFilter1);
		}else{
			if (StringUtil.isNotBlank(model.getStartTime())) {
				Date start = DateUtil.valueOf(model.getStartTime() + " 00:00:00");
				param.addParam("addtime", Operators.GTE, start);
			}
			if (StringUtil.isNotBlank(model.getEndTime())) {
				Date end = DateUtil.valueOf(model.getEndTime() + " 23:59:59");
				param.addParam("addtime", Operators.LTE, end);
			}
			if (StringUtil.isNotBlank(model.getAccount())) {
				param.addParam("account", Operators.EQ, model.getAccount());
			}
			if (StringUtil.isNotBlank(model.getToAccount())) {
				param.addParam("payAccount", Operators.EQ, model.getToAccount());
			}
			if (model.getStatus() != 99){
				param.addParam("status", Operators.EQ, model.getStatus());
			}
			if(model.getType() != 99){
				param.addParam("type", Operators.EQ, model.getType());
			}
			if(StringUtil.isNotBlank(model.getOrderId())){
				param.addParam("orderId", Operators.EQ, model.getOrderId());
			}
		}
		param.addOrder(OrderType.DESC, "id");
		PageDataList<TppMerchantLog> pageDataList = this.findPageList(param);
		PageDataList<TppMerchantLogModel> pageDataList_ = new PageDataList<TppMerchantLogModel>();
		List<TppMerchantLogModel> list = new ArrayList<TppMerchantLogModel>();
		pageDataList_.setPage(pageDataList.getPage());
		if (pageDataList.getList().size() > 0) {
			for (int i = 0; i < pageDataList.getList().size(); i++) {
				TppMerchantLog log = (TppMerchantLog) pageDataList.getList().get(i);
				TppMerchantLogModel logModel = TppMerchantLogModel.instance(log);
				logModel.setOpName(log.getOperator().getUserName());
				list.add(logModel);
			}
		}
		pageDataList_.setList(list);
		return pageDataList_;
	}

}
